package com.szqhyxj.youxiangju.biz.set.forgetpassword;

import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.PhonenumberUtil;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/31.
 */

public class ForgetPassWordPresenter implements ForgetPassWordContract.Presenter {
    ForgetPassWordContract.View view;

    private ForgetPassWordModel.ForgetPasswordEntity forgetPasswordEntity;

    public ForgetPassWordPresenter(ForgetPassWordContract.View view) {
        this.view = view;
        forgetPasswordEntity = new ForgetPassWordModel.ForgetPasswordEntity();
    }

    @Override
    public void onDestroy() {

    }

    private void checkRegisterEnable() {
        if (forgetPasswordEntity.valid()) {
            view.showChangeEnable();
        } else {
            view.showChangeDisable();
        }
    }

    @Override
    public void setPhone(String phone) {
        forgetPasswordEntity.setUseraccount(phone);
        checkRegisterEnable();

    }

    @Override
    public void setCode(String code) {
        forgetPasswordEntity.setValidCode(code);
        checkRegisterEnable();
    }

    @Override
    public void setPassword(String password) {
        forgetPasswordEntity.setLoginPwd(password);
        checkRegisterEnable();
    }

    @Override
    public void setVerifyPassword(String verifyPassword) {
        forgetPasswordEntity.setRegisterVerifyPassword(verifyPassword);
        checkRegisterEnable();
    }

    private static final int MINUTE = 60;
    private ArrayList<Disposable> disposables = new ArrayList<>();

    @Override
    public void acquireVerificationCode() {
        if (forgetPasswordEntity.getUseraccount() == null || forgetPasswordEntity.getUseraccount().equals("")) {
            view.showFali("请填写手机号码！");
        }
        if (!PhonenumberUtil.isMobileNO(forgetPasswordEntity.getUseraccount())) {
            view.showFali("请填写正确手机号码！");
        } else {
            view.showLoading();
            disposables.add(ForgetPassWordModel.sms(this.forgetPasswordEntity)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            new Consumer<BaseResult>() {
                                @Override
                                public void accept(BaseResult baseResult) {
                                    view.dismissLoading();
                                    if (baseResult.businessLogicSuccess()) {
                                        view.showAcquiringVerificationCode(MINUTE);
                                        view.showAcquiredVerificationCodeSuccess();
                                        startCountDown();
                                    } else {
                                        view.showAcquiredVerificationCodeFail(baseResult.getMessage());
                                    }
                                }
                            },
                            new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) {
                                    view.dismissLoading();
                                    view.showAcquiredVerificationCodeFail(throwable.getMessage());
                                }
                            }
                    )
            );
        }

    }


    private void startCountDown() {
        disposables.add(Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        if (aLong == MINUTE - 1) {
                            view.showAcquiredVerificationCode();
                        } else {
                            view.showAcquiringVerificationCode((int) (MINUTE - aLong - 1));
                        }
                    }
                }));
    }

    @Override
    public void registered() {
        ForgetPassWordModel.commit(this.forgetPasswordEntity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>() {
                               @Override
                               public void accept(BaseResult baseResult) throws Exception {
                                   //成功
                                   if (baseResult.businessLogicSuccess()) {
                                       view.showToast("找回密码成功！");
                                       view.showSuccess();

                                   } else {
                                       view.showToast(baseResult.getMessage() + "");
                                   }


                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //失败
                                view.showFali(throwable.getMessage());
                            }
                        });
    }


}
